define(["jquery","servers"], function ($,servers) {
    return Backbone.Model.extend({
        defaults: function () {
            return {
                defListStatus:[{ id: "1", name: "有效" },{ id: "2", name: "暂停中" }]
            };
        },
        getPageList:function(param){
            var self = this;
            var paramMap=servers.getPageParamMap();
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.agent.pageList(newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        var newInfo= $.ExtCommon.getMappingJson(info,{
                            id:"id",name:"name",status:"status",commission:"rebate",customerNum:"advertiserNumber",createTime:"createTime"
                        },true);
                        if(!$.ExtCommon.isCheckEmpty(newInfo.createTime)){newInfo.createTime=$.ExtMoment.initFormat(newInfo.createTime)}
                        return newInfo;
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataPageList: res});
                self.trigger('bind:PageList');
            });
        },
        getAllList:function () {
            var self = this;
            servers.agent.list(function(res){
                var newData=[];
                if(res.success){
                    newData=$.map(res.data,function (info) {
                        var newInfo = $.ExtCommon.getMappingJson(info, {id: "id", name: "name"}, true);
                        newInfo.nameExt = newInfo.id + "_" + newInfo.name;
                        return newInfo;
                    });
                }
                res.data=newData;
                self.set({dataAllList: res});
                self.trigger('bind:AllList');
            });
        },
        getCustomerNum:function(id) {
            var self = this;
            if (id) {
                servers.agent.customerNum({agentId:id}, function (res) {
                    var newJson = $.ExtCommon.getMappingJson(res.success ? res.data : {}, {
                        id: "id",number: "number"
                    }, true);
                    res.dataOriginal = res.data;
                    res.data = newJson;
                    self.set({dataInfo2: {isAdd: false, res: res}});
                    self.trigger('bind:RebateDisable');
                });
            }else {
                var dataDef={number:0};
                self.set({dataInfo2:{isAdd:true,res:servers.getSendSuccess(dataDef)}});
                self.trigger('bind:RebateDisable');
            }
        },
        getInfo: function (id) {
            var self = this;
            if (id) {
                servers.agent.info({agentId:id}, function (res) {
                    var newJson = $.ExtCommon.getMappingJson(res.success ? res.data : {}, {
                        id: "id",name: "name",status: "status",commission: "rebate",contactName: "linkName",phone: "phoneNo",email: "email"
                    }, true);
                    res.dataOriginal = res.data;
                    res.data = newJson;
                    self.set({dataInfo: {isAdd: false, res: res}});
                    self.trigger('bind:Info');
                });
            }else {
                var dataDef={status:"1"};
                self.set({dataInfo:{isAdd:true,res:servers.getSendSuccess(dataDef)}});
                self.trigger('bind:Info');
            }
        },
        addInfo: function (json) {
            var self = this;
            var data=this._getMappingToServer(json);
            servers.agent.add(data, function (res) {
                self.set({dataSave: {isAdd: true, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        updateInfo: function (json) {
            var self = this;
            var data=this._getMappingToServer(json);
            data.id=json.id;
            servers.agent.update(data, function (res) {
                self.set({dataSave: {isAdd: false, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        _getMappingToServer:function(json){
            var newJson = $.ExtCommon.getMappingJson(json, {
                id: "id",name: "name",status: "status",rebate: "commission",linkName: "contactName",phoneNo: "phone",email: "email"
            });
            return newJson
        }
    });
});